﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;

namespace RS.DOM.Types
{
    public class Bool: Token
    {
        public Bool(Context context, bool res)
            : base(context)
        {
            base.TokenType = TokenType.Bool;
            base.Result = res;
        }

        public override Token Operation(Token token, OperationType type)
        {
            if (type == OperationType.Compare)
            {
                if (this.Result)
                    return token;
                else
                    return new Null(this.Context);
            }
            else if (type == OperationType.And)
            {
                if (token.TokenType == TokenType.Bool)
                {
                    Bool op1 = this;
                    Bool op2 = (Bool)token;

                    return new Bool(this.Context, op1.Result && op2.Result);
                }
                else
                {
                    return this;
                }
            }
            else if (type == OperationType.Or)
            {
                if (token.TokenType == TokenType.Bool)
                {
                    Bool op1 = this;
                    Bool op2 = (Bool)token;

                    return new Bool(this.Context, op1.Result || op2.Result);
                }
                else
                {
                    return this;
                }
            }
            else if (type == OperationType.Apply)
            {
                if (token.TokenType == TokenType.Bool)
                {
                    Bool op1 = this;
                    Bool op2 = (Bool)token;

                    op1.Result = op2.Result;

                    return op1;
                }
                else
                {
                    return this;
                }
            }
            else
            {
                return base.Operation(token, type);
            }
        }

        public bool Result
        {
            get
            {
                return (bool)base.Result;
            }

            set
            {
                base.Result = value;
            }
        }

        public override Token Create()
        {
            return new Bool(this.Context, this.Result);
        }
    }
}
